/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package maindatart.view;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import maindatart.maindata.MainDataRashService;
import maindatart.metadata.MDMetaDataService;
import maindatart.metadata.MainDataMetaDataEntity;
import maindatart.metadata.MainDataPropertyEntity;
import maindatart.rush.config.MainDataRashConfig;
import maindatart.rush.config.RushConfigService;
import maindatart.rush.result.RushReportEntity;
import maindatart.rush.result.RushResultService;

/**
 *
 * @author Administrator
 */
public class RushPanel extends javax.swing.JPanel {

    private List<MainDataMetaDataEntity> mdmdList = null;
    private List<MainDataRashConfig> configs = null;
    private MDMetaDataService service = new MDMetaDataService();
    private RushConfigService rcService = new RushConfigService();
    private MainDataRashService mdrService = new MainDataRashService();
    private RushResultService rrs = new RushResultService();

    /**
     * Creates new form RushPanel
     */
    public RushPanel() {
        initComponents();
        initData();
    }

    private void initData() {
        setJpanel1Data();
    }

    private void setJpanel1Data() {

        DefaultListModel dlm = new DefaultListModel();
        try {
            mdmdList = service.getAllRegMainData();
            for (MainDataMetaDataEntity mdmd : mdmdList) {
                String name = mdmd.getMainDataName();
                dlm.addElement(name);
            }
            jList3.setModel(dlm);
            jList3.repaint();
        } catch (Exception ex) {
            Logger.getLogger(MainDataRegPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup3 = new javax.swing.ButtonGroup();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jLabel2.setText("清洗报告");

        jLabel3.setText("待清洗数据");

        jList3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                showRushAlg(evt);
            }
        });
        jScrollPane6.setViewportView(jList3);

        jLabel4.setText("主数据列表");

        jTextField1.setToolTipText("");

        jButton4.setText("清洗");
        jButton4.setToolTipText("");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "清洗编号", "清洗时间", "状态", "备注"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("历史清洗记录");

        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                showAlg(evt);
            }
        });
        jScrollPane3.setViewportView(jList1);

        jLabel5.setText("清洗算法列表");

        jLabel6.setText("算法编号");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5)
                            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel6)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButton4))
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane2)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE))
                            .addComponent(jLabel2))
                        .addContainerGap(64, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void showRushAlg(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showRushAlg
        String currentName = (String) jList3.getSelectedValue();
        jTextField1.setText(currentName);
        try {
            configs = rcService.getMainDataRashConfigs(currentName);
            DefaultListModel dlm = new DefaultListModel();

            for (MainDataRashConfig c : configs) {
                String id = c.getId();
                dlm.addElement(id);
            }
            jList1.setModel(dlm);
            jList1.repaint();
            this.loadRushRecord();
        } catch (Exception ex) {
            Logger.getLogger(AlgDesignPanel.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this.getParent(), "出问题了" + ex.getMessage());
        }
    }//GEN-LAST:event_showRushAlg

    private void showAlg(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showAlg
        String currentAlg = (String) jList1.getSelectedValue();
        jTextField2.setText(currentAlg);
    }//GEN-LAST:event_showAlg

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        String maindataname = jTextField1.getText();
        String algName = jTextField2.getText();
        
        if (maindataname == null || maindataname.length() <= 0) {
            JOptionPane.showMessageDialog(this.getParent(), "请选择主数据");
            return;
        }
        if (algName == null || algName.length() <= 0) {
            JOptionPane.showMessageDialog(this.getParent(), "请选择算法");
            return;
        }
        MainDataRashConfig mdrc = null;
        try {
            for (MainDataRashConfig mdr : configs) {
                if (algName.equals(mdr.getId())) {
                    mdrc = mdr;
                }
            }

            String enName = null;
            for (MainDataMetaDataEntity mdmd : mdmdList) {
                if (mdmd.getMainDataName().equals(maindataname)) {
                    enName = mdmd.getEnName();
                }
            }
            
            PrintStream oldPrintStream = new PrintStream(System.out);
            TextAreaPrintStream out = new TextAreaPrintStream(jTextArea1);            
            System.setOut(out);
            Date d1 = new Date();
//            RushReportEntity rre = mdrService.rushData(enName, mdrc);
            RushReportEntity rre = mdrService.rushMainData(enName, mdrc);
            System.setOut(oldPrintStream);
            Date d2 = new Date();
            JOptionPane.showMessageDialog(this.getParent(), "清洗完毕!用时：" + (d2.getTime() - d1.getTime()) + "毫秒");
            
            loadRushRecord();
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this.getParent(), "清洗出现异常" + ex.getMessage());
            Logger.getLogger(RushPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void loadRushRecord(){
        String maindataname = jTextField1.getText();
        String enName = null;
            for (MainDataMetaDataEntity mdmd : mdmdList) {
                if (mdmd.getMainDataName().equals(maindataname)) {
                    enName = mdmd.getEnName();
                }
            }
        try {
            List<RushReportEntity> list = rrs.getRushReportEntitylist(enName);
            DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
            tm.setRowCount(0);
            for(RushReportEntity rre : list){
                Vector v = new Vector();
                v.add(rre.getId());
                v.add(rre.getTime());
                v.add(rre.getMainDataName());
                v.add(rre.getInfo());
                tm.addRow(v);
            }
            jTable1.setModel(tm);
            jTable1.setEnabled(true);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this.getParent(), "读取清洗记录出现异常" + ex.getMessage());
            Logger.getLogger(RushPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JList jList3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    private static class TextAreaPrintStream extends PrintStream {

        private JTextArea jta;
        private static final OutputStream DUMMY_OUT = new ByteArrayOutputStream(0);

        public TextAreaPrintStream(JTextArea jta) throws FileNotFoundException {
            super(DUMMY_OUT);
            this.jta = jta;
        }

        public void print(String s) {
            if (s == null) {
                s = "null";
            }
            jta.append(s);
            jta.repaint();
        }
        
        public void println(String s) {
            if (s == null) {
                s = "null";
            }
            jta.append(s + "\n" );
            jta.repaint();
        }
        /**
         * 21. * 实现其他的print和println方法，使得所有打印方法最终通过print(String s) 22. * ... 23.
         */

    }
}
